package com.ztools.pc.service.impl.sys;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ztools.common.model.pc.sys.Dict;
import com.ztools.common.model.pc.sys.DictData;
import com.ztools.common.util.common.PageUtil;
import com.ztools.pc.mapper.sys.DictMapper;
import com.ztools.pc.service.sys.DictDataService;
import com.ztools.pc.service.sys.DictService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 一口香菜
 * @Date: 2023/3/3  17:35
 * @Version 1.0
 */
@Transactional
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Lazy
    @Resource
    private DictDataService dictDataService;

    @Override
    public Page<Dict> findList(PageUtil<Dict> pageUtil) {
        List<OrderItem> orders = new ArrayList<>();
        QueryWrapper<Dict> queryWrapper = getQueryWrapper(pageUtil.getParams());
        Page<Dict> pages = new Page<>(pageUtil.getPage(), pageUtil.getSize());
        // 排序数据  StrUtil.toUnderlineCase huTools将驼峰装换为下划线格式
        orders.add(new OrderItem(StrUtil.toUnderlineCase(pageUtil.getSortName()), "asc".equals(pageUtil.getSortOrder())));
        pages.setOrders(orders);
        return page(pages, queryWrapper);
    }

    @Override
    public List<DictData> findByObject(Dict dict) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>(dict);
        Dict dictList = getOne(queryWrapper);
        DictData dictData = DictData.builder().dictId(dictList.getId()).build();
        return dictDataService.findByObject(dictData);
    }

    @Override
    public String findByTypeCode(String type, String code) {
        Dict dict = Dict.builder().type(type).build();
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>(dict);
        Dict d = getOne(queryWrapper);
        if (d != null) {
            DictData dictData = DictData.builder().dictId(d.getId()).code(code).build();
            List<DictData> dictDataList = dictDataService.findByObject(dictData);
            if (dictDataList != null) {
                return dictDataList.get(0).getValue();
            }
        }
        return null;
    }

    @Override
    public String findByTypeCodes(String type, String codes) {
        List<DictData> dictDataList = null;
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type);
        Dict dict = getOne(queryWrapper);
        if (dict != null) {
            dictDataList = dictDataService.findByTypeCodes(dict.getId(), codes);
        }
        if (dictDataList != null) {
            List<String> valueList = dictDataList.stream().map(DictData::getValue).collect(Collectors.toList());
            return String.join(",", valueList);
        }
        return null;
    }

    @Override
    public boolean delete(Dict dict) {
        boolean b = false;
        // 删除前先获取，在删除（乐观锁）
        Dict dictById = getById(dict);
        b = removeById(dict.getId());
        return b;
    }

    @Override
    public boolean update(Dict dict) {
        boolean b = false;
        // 修改前先获取，在删除（乐观锁）
        Dict dictById = getById(dict);
        b = updateById(dict);
        return b;
    }

    public QueryWrapper<Dict> getQueryWrapper(Dict dict) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotEmpty(dict.getName())) {
            queryWrapper.lambda().like(Dict::getName, dict.getName());
        }
        if (StrUtil.isNotEmpty(dict.getType())) {
            queryWrapper.lambda().like(Dict::getType, dict.getType());
        }
        return queryWrapper;
    }

}
